# ------------------------------------------------------------------------------
# Replication Materials
# 
# title: Eliciting Beliefs as Distributions in Online Surveys
# journal: Political Analysis
# authors: Lucas Leemann, Richard Traunmüller, and Lukas Stoetzer
# date: August 2020
# ------------------------------------------------------------------------------




library(tidyverse)
library(xtable)
options(xtable.comment = FALSE)
source("fun/fun_electbeleifs_MLE.R")
source("fun/fun_utilities.R")



source("00_DataCleaner_symmetric.R") # Richard Code to clean Data
dat_sym <- filter(data, screen1==1, screen2==1) # Filter with screen questions

source("00_DataCleaner_asymmetric.R") # Richard Code to clean Data
dat_asym <- filter(data, screen1==1, screen2==1) # Filter with screen questions

source("00_DataCleaner_symmetric_variance.R") # Richard Code to clean Data
dat_sym_var <- filter(data, screen1==1, screen2==1) # Filter with screen questions

source("00_DataCleaner_asymmetric_variance.R") # Richard Code to clean Data
dat_asym_var <- filter(data, screen1==1, screen2==1) # Filter with screen questions

source("00_DataCleaner_nachtrag_sym.R") # Richard Code to clean Data
dat_nach_sym <- filter(data, screen1==1, screen2==1) # Filter with screen questions

source("00_DataCleaner_nachtrag_asym.R") # Richard Code to clean Data
dat_nach_asym <- filter(data, screen1==1, screen2==1) # Filter with screen questions


# Estimation  ============

res <- list() # Result List

#+ Quantile, echo=FALSE, include=FALSE
## Quantile Question  ============

# Estimate Model for Quantile Question Symmetric, Low Variance
res[["sym_lvar_quant"]] <- dat_sym %>%
  creat_dat_Y(method="Quantile") %>%
  est_eB_beta(true.val = c(60,60))

# Estimate Model for Quantile Question Asymmetric, Low Variance
res[["asym_lvar_quant"]]  <- dat_asym %>%
  creat_dat_Y(method="Quantile") %>%
  est_eB_beta(true.val = c(60,30))

#+ Quantile2, echo=FALSE, include=FALSE
## Quantile Question (without correction)  ============

# # Estimate Model for Quantile Question Symmetric, Low Variance
res[["sym_lvar_qtnsc"]] <- dat_nach_sym %>%
  creat_dat_Y(method="QuantileNoCorrect") %>%
  est_eB_beta(true.val = c(60,60))

# Estimate Model for Quantile Question Asymmetric, Low Variance
res[["asym_lvar_qtnsc"]]  <- dat_nach_asym %>%
  creat_dat_Y(method="Quantile") %>%
  est_eB_beta(true.val = c(60,30))

#+ Table, echo=FALSE, include=FALSE
## Table with Estimates  ============


# Data Frame
df <- data.frame(t(sapply(res, "[[", "par")))
rownames(df) <- NULL
df$Variance <- c("Large Variance")
df$Distribution <- c("Symmetric","Asymmetric")
df$AdequacyCheck <- c("Yes","Yes","No","No")
df$N <- sapply(res, "[[", "n")

# True values
df_true <- data.frame(Variance = "Large Variance",
                    Distribution = c("Symmetric","Asymmetric"),
                    alpha_true = c(60,60), beta_true = c(60,30))

# xtab 
xtab <- left_join(df, df_true) %>%
          rowwise() %>%
          mutate(KL = KL_dis_beta(c(alpha_true,beta_true), as.numeric(c(alpha,beta)))) %>%
          select(Variance, Distribution, AdequacyCheck, alpha, beta, KL, N) %>%
          arrange(Distribution)

print(xtable(xtab, caption="Estimates for the Elicited Beliefs. 
             Comparing Quantile with and without Adequacy Check for the Large Variance Scenarios"), 
      booktabs = T, include.rownames=FALSE,
      file="out/fig/Tab_A5.tex")



